package com.axend.sofa.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import cn.com.heaton.blelibrary.ble.model.BleDevice
import com.axend.lib_base.base.vm.BaseViewModel
import com.axend.lib_base.bean.SofaModelEntity
import com.axend.lib_base.engine.toast.toast
import com.axend.lib_base.ext.commContext
import com.axend.lib_base.help.ble.BleClient
import com.axend.lib_base.help.ble.BleMsgProcessor
import com.axend.lib_base.help.ble.enums.BleCommandTag
import com.axend.lib_base.utils.ByteUtil
import com.axend.lib_base.utils.CommUtils
import com.axend.lib_base.utils.CommUtils.getString
import com.axend.lib_base.utils.log.MyLogUtils
import com.axend.lib_base.view.dialog.CustomCalibrationDialog
import com.axend.sofa.R

class SelectModelViewModel : BaseViewModel() {

    /*型号数据*/
    private var _modelData: MutableLiveData<List<SofaModelEntity>> = MutableLiveData()
    val modelData: LiveData<List<SofaModelEntity>> get() = _modelData

    /*校准按钮是否能点击 默认无法点击*/
    private var _calibrationEnable: MutableLiveData<Boolean> = MutableLiveData()
    val calibrationEnable: LiveData<Boolean> get() = _calibrationEnable

    /**
     * dialogModel 0 loading 1 failed 2 success 3 dismiss
     */
    private var _dialogModel:MutableLiveData<Int> = MutableLiveData()
    val dialogModel: MutableLiveData<Int> get() = _dialogModel


    /*添加型号数据*/
    fun addModelData(dataList: List<SofaModelEntity>) {
        launchOnUI {
            _modelData.postValue(dataList)
        }
    }


    fun writeBleMsg(msg: ByteArray, originData: Any = "") {
        launchOnUI {
            BleClient.writeData(msg, object : BleClient.OnBleWriteCallback {
                override fun onWriteSuccess(device: BleDevice, data: ByteArray) {
                    /*写入成功后返回的消息处理*/
                    val msgPair = BleMsgProcessor.decodeMsg(data)
                    msgPair?.let {
                        when (msgPair.first) {
                            BleCommandTag.SET_SOFA_MODEL -> {
                                when (ByteUtil.bytes2IntLittle(msgPair.second)) {
                                    1 -> {
                                        setCalibrationEnable(originData!=0)
                                        toast(CommUtils.getString(com.axend.lib_base.R.string.app_tips_set_success))
                                    }

                                    0 -> {
                                        setCalibrationEnable(false)
                                        toast(CommUtils.getString(com.axend.lib_base.R.string.app_tips_set_failed))
                                    }
                                }
                            }

                            BleCommandTag.STUDY_RESULT -> {
                                loadHideProgress()
                                setDialogModel(CustomCalibrationDialog.MODEL_SUCCESS)
                                val thresh1Byte = byteArrayOf(msgPair.second[0], msgPair.second[1],
                                    msgPair.second[2], msgPair.second[3])
                                val thresh2Byte = byteArrayOf(msgPair.second[4], msgPair.second[5],
                                    msgPair.second[6], msgPair.second[7])
                                MyLogUtils.w("thresh1Byte:${ByteUtil.byte4ToFloatLittle(thresh1Byte)}" +
                                        ",thresh2Byte:${ByteUtil.byte4ToFloatLittle(thresh2Byte)}")
                            }

                            BleCommandTag.SET_STUDY_START -> {
                                when (msgPair.second[0]) {
                                    0.toByte() -> {
                                        loadHideProgress()
                                        setDialogModel(CustomCalibrationDialog.MODEL_FAILED)
                                    }

                                    1.toByte() -> {
//                                        setDialogModel(CustomCalibrationDialog.MODEL_LOADING)
                                        loadStartCalibration()
                                    }
                                }
                            }
                            BleCommandTag.SET_STUDY_END -> {
                                when (msgPair.second[0]) {
                                    0.toByte() -> {
                                        loadHideProgress()
                                        setDialogModel(CustomCalibrationDialog.MODEL_FAILED)
                                    }

                                    1.toByte() -> {
                                        loadHideProgress()
                                        setDialogModel(CustomCalibrationDialog.MODEL_DISMISS)
                                    }
                                }
                            }

                            else -> {}
                        }
                    }
                }

                override fun onWriteFailed(device: BleDevice, errorCode: Int) {

                }

            })
        }
    }


    /**
     * 设置校准按钮是否能点击
     */
    fun setCalibrationEnable(enable: Boolean) {
        launchOnUI {
            _calibrationEnable.postValue(enable)
        }
    }


    /**
     * 设置dialogModel
     */
    fun setDialogModel(model: Int) {
        launchOnUI {
            _dialogModel.postValue(model)
        }
    }

}